/* This short C program will adjust the repeat rate of your
 * 101/102 key keyboard.  You can add it, with appropriate parameters,
 * to your AUTOEXEC.BAT file to set the key repeat rate every time
 * you boot your computer.  See the help() function for parameter
 * information.  Compile this program with MSC 5.x, QC 2.0, or TC 2.0
 */

#include <stdio.h>
#include <stdlib.h>
#include <dos.h>

void testkb(void);
void help(void);

void main(int argc, char *argv[])
{
   int delay, rate;
   union REGS regs;

   if(argc != 3)              /* Must be two command-line parameters */
      help();

   delay = atoi(argv[1]);     /* Get delay rate */
   if (delay<0 || delay>3)    /* Delay must be 0 to 3 */
      help();

   rate = atoi(argv[2]);      /* Get repeat rate */
   if (rate<0 || rate>31)     /* Rate must be 0 to 31 */
      help();

   testkb();                  /* Do we have a 101-key KB? */
   
   regs.h.ah = 3;             /* Service 3 */
   regs.h.al = 5;             /* Subservice 5 (0-4 are PCjr) */
                              /* NOTE: Some versions of Phoenix BIOS have */
                              /* a bug that requires you to use 6 instead */
                              /* of 5 for the subservice number */

   regs.h.bh = (char) delay;  /* Set new values */
   regs.h.bl = (char) rate;
   int86(0x16, &regs, &regs); /* Let BIOS set keyboard */

   puts("New keyboard typematic values have been set");
}

void testkb()
{
   char far *fp;

   fp = (void far *) 0x496L;
   if ((*fp | 0x10) == 0)
      {
      puts("You must have a 101/102 key keyboard and supporting BIOS");
      puts("to use this program.");
      exit(-1);
      }
}

void help()
{
   puts("   This program sets the typematic (key repeat) rate on computers");
   puts("with 101/102 key keyboards.  To use the program from the DOS");
   puts("prompt, type");
   puts("             KEYREPT n1 n2");
   puts("");
   puts("  n1 is a number between 0 and 3.  It sets how long the computer");
   puts("pauses before repeating keystrokes.");
   puts("    n1 = 0  ==>  1/4 sec. pause");
   puts("    n1 = 1  ==>  1/2 sec. pause");
   puts("    n1 = 2  ==>  3/4 sec. pause");
   puts("    n1 = 3  ==>  1   sec. pause");
   puts("");
   puts("  n2 is a number between 0 and 31.  It sets how fast keystrokes");
   puts("repeat when a key is held down.");
   puts("    n2 =  0  ==>  30 characters per second (fast)");
   puts("    n2 = 31  ==>   2 characters per second (slow)");
   puts("Other values are scaled between these extremes.");
   puts("  For example, you can set the fastest typematic rate by typing");
   puts("             KEYREPT 0 0");
   exit(-1);
}



